package com.bub.pay.outer.redis.redis;

import com.bub.pay.framework.common.async.ExceptionHandlingAsyncTaskExecutor;
import com.bub.pay.framework.redis.RedisClient;
import com.bub.pay.framework.redis.model.SmsModel;
import com.bub.pay.outer.redis.sms.SmsSender;
import com.bub.pay.outer.redis.AsyncSmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

/**
 * 
 * @ClassName: RedisSmsListener
 * @Description: redis监听
 * @author Leo
 * @version V1.0
 */
public class RedisSmsListener implements MessageListener {

	/** Logger */
	private static final Logger log = LoggerFactory.getLogger(RedisSmsListener.class);

	@Autowired
	private RedisClient redisClient;
	@Autowired
	private ExceptionHandlingAsyncTaskExecutor taskExecutor;

	@Autowired
	private SmsSender smsSender;

	@Override
	public void onMessage(Message message, byte[] pattern) {
		if (null == message) {
			return;
		}
		Object obj = redisClient.getRedisTemplate().getDefaultSerializer().deserialize(message.getBody());
		if (obj instanceof SmsModel) {
			final SmsModel model = (SmsModel) obj;
			log.info("Send Sms to:" + model.getPhoneNum() + ",Template:" + model.getTemplateId());
			taskExecutor.execute(new AsyncSmsService(model,smsSender));
		}
	}

}
